#include <Windows.h>

#include <QString>

#include "FileHooker.h"
#include "JmpHooker.h"
#include "Constant.h"



HANDLE WINAPI hook_CreateFileW(decltype(&CreateFileW) real_CreateFileW,
    _In_ LPCWSTR lpFileName,
    _In_ DWORD dwDesiredAccess,
    _In_ DWORD dwShareMode,
    _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    _In_ DWORD dwCreationDisposition,
    _In_ DWORD dwFlagsAndAttributes,
    _In_opt_ HANDLE hTemplateFile
    )
{
    auto filename = QString::fromUtf16((ushort *)lpFileName);

    if (dwCreationDisposition != OPEN_EXISTING)
    {
        if (filename.right(4) == ".php")
        {
            filename = "";
        }
    }
    return real_CreateFileW((const wchar_t *)filename.utf16(),
        dwDesiredAccess,
        dwShareMode,
        lpSecurityAttributes,
        dwCreationDisposition,
        dwFlagsAndAttributes,
        hTemplateFile);
}


XProcess::JmpHook<decltype(&CreateFileW)> h_CreateFileW;

void FileHooker::hookAll()
{
    h_CreateFileW = XProcess::JmpHooker<(int)Constants::HookIds::CreateFileW, decltype(&CreateFileW)>::hook(&CreateFileW, &hook_CreateFileW);
}

void FileHooker::unhookAll()
{
    h_CreateFileW.unHook();
}
